package com.example.myapplication.teenager;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.myapplication.Config.TokenBean;
import com.example.myapplication.R;
import com.example.myapplication.ShuJu.BotYIzhanBean;
import com.example.myapplication.ShuJu.LibraryDetailsBean;
import com.example.myapplication.ShuJu.Re_NewsHots;
import com.example.myapplication.ShuJu.ToPYizhanBean;
import com.example.myapplication.apter.BotYIzhanAdapter;
import com.example.myapplication.apter.NewsAdapter;
import com.example.myapplication.apter.NewsHotApter;
import com.example.myapplication.apter.YiZhanApter;
import com.google.gson.Gson;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class YouthStationActivity extends AppCompatActivity {

    private ImageView imageView;
    private TextView descriptionTextView;
    private RecyclerView cen_yizhan;
    private RecyclerView bot_yizhan;
    private OkHttpClient okHttpClient;
    private Request  request;
    private Gson gson;
    private ToPYizhanBean toPYizhanBean;
    private BotYIzhanBean botYIzhanBean;
    private Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_youth_station);
        initview();
        //上部
        ToPYizhanBean();
        //下部
        botYIzhanBean();

    }

    private void botYIzhanBean() {
        request = new Request.Builder()
                .url("http://124.93.196.45:10001/prod-api/api/youth-inn/youth-inn/list")
                .addHeader("Authorization", TokenBean.token)
                .build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {

            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                String s = response.body().string();
                Gson gson = new Gson();
                botYIzhanBean = gson.fromJson(s, BotYIzhanBean.class);
                Log.d("toPYizhanBean",s);
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                       binyizhan();
                    }
                });
            }
        });
    }

    private void binyizhan() {
        BotYIzhanAdapter botYIzhanAdapter = new BotYIzhanAdapter(this,botYIzhanBean.getRows());
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        bot_yizhan.setLayoutManager(linearLayoutManager);
        bot_yizhan.setAdapter(botYIzhanAdapter);
    }


    private void ToPYizhanBean() {
        request = new Request.Builder()
                .url("http://124.93.196.45:10001/prod-api/api/youth-inn/talent-policy-area/list")
                .addHeader("Authorization",TokenBean.token)
                .build();
         okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {

            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                String s = response.body().string();
                Gson gson = new Gson();
                toPYizhanBean = gson.fromJson(s, ToPYizhanBean.class);
                Log.d("toPYizhanBean",s);
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        Glide.with(YouthStationActivity.this).load(toPYizhanBean.getData().get(0).getImgUrl()).into(imageView);
                        descriptionTextView.setText(toPYizhanBean.getData().get(0).getName());
                        //中部
                        cenYizhanBean();
                    }
                });
            }
        });
    }

    private void cenYizhanBean() {
        YiZhanApter yiZhanApter = new YiZhanApter(this,toPYizhanBean.getData());
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this,3);
        cen_yizhan.setLayoutManager(gridLayoutManager);
        cen_yizhan.setAdapter(yiZhanApter);
    }


    private void initview() {
        imageView = findViewById(R.id.imageView);
        descriptionTextView = findViewById(R.id.descriptionTextView);
        cen_yizhan = findViewById(R.id.cen_yizhan);
        bot_yizhan = findViewById(R.id.bot_yizhan);

        gson = new Gson();
        okHttpClient = new OkHttpClient();
        handler = new Handler();

    }
}